package com.zhihuichongcheng.application.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zhihuichongcheng.application.R;


/**
 * Created by 3water on 2016/10/27.
 */

public class CustomWebviewActivity extends BaseActivity {
    private WebView mWebView;

    private String mUrl, mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initView();
        initData();
    }

    private void initView() {
        mWebView = (WebView) findViewById(R.id.webview);
        WebSettings settings = mWebView.getSettings();
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        mWebView.setWebChromeClient(new WebChromeClient() {

        });

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        //允许混合内容 解决部分手机 加载不出https请求里面的http下的图片
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("title")) {
            mTitle = intent.getStringExtra("title");
        }
        if (intent != null && intent.hasExtra("url")) {
            mUrl = intent.getStringExtra("url");
        }
        if (!TextUtils.isEmpty(mTitle)) {
            setTitle(mTitle);
        }
        if (!TextUtils.isEmpty(mUrl)) {
            mWebView.loadUrl(mUrl);
        }
    }
}
